package com.frxuexi.shop.viewmodel

import com.frxuexi.base.db.OrderDao
import com.frxuexi.base.http.ShopApiService
import com.frxuexi.base.mvi.BaseViewModel
import com.frxuexi.base.mvi.RequestType
import com.frxuexi.shop.intent.CartIntent
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.flow
import javax.inject.Inject

@HiltViewModel
class CartViewModel @Inject constructor(val apiService: ShopApiService,val orderDao: OrderDao):BaseViewModel<CartIntent>() {
    override fun handleIntent(it: CartIntent) {
        when(it){
            is CartIntent.getCarts->httpRequest(apiService.getCarts(it.page))
            is CartIntent.updateCart->httpRequest(apiService.updateCart(it.id,it.num),RequestType.UPDATE)
            is CartIntent.deleteCart->httpRequest(apiService.deleteCart(it.ids),RequestType.DELETE)
            is CartIntent.addOrder->roomRequest(flow { emit(orderDao.addOrder(it.orderEntity)) },RequestType.ADD)
            is CartIntent.preOrder->httpRequest(apiService.preOrder(toBody(it.bean)),RequestType.PREORDER)
            is CartIntent.getPreOrder->httpRequest(apiService.getPreOrder(it.no),RequestType.GETPREORDER)
            is CartIntent.createOrder->httpRequest(apiService.createOrder(toBody(it.bean)),RequestType.CREATEORDER)
        }
    }
}